<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="报告作者ID" prop="author">
      <el-input v-model="dataForm.author" placeholder="报告作者ID"></el-input>
    </el-form-item>
    <el-form-item label="报告名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="报告名称"></el-input>
    </el-form-item>
    <el-form-item label="报告资源存储url" prop="url">
      <el-input v-model="dataForm.url" placeholder="报告资源存储url"></el-input>
    </el-form-item>
    <el-form-item label="创建者ID" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建者ID"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改者ID" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="修改者ID"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="逻辑删除"></el-input>
    </el-form-item>
    <el-form-item label="版本号" prop="version">
      <el-input v-model="dataForm.version" placeholder="版本号"></el-input>
    </el-form-item>
    <el-form-item label="组织机构代号" prop="corpCode">
      <el-input v-model="dataForm.corpCode" placeholder="组织机构代号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          author: '',
          name: '',
          url: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: '',
          delFlag: '',
          version: '',
          corpCode: ''
        },
        dataRule: {
          author: [
            { required: true, message: '报告作者ID不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '报告名称不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: '报告资源存储url不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建者ID不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '修改者ID不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '逻辑删除', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号不能为空', trigger: 'blur' }
          ],
          corpCode: [
            { required: true, message: '组织机构代号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/achievements/researchreport/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.author = data.researchReport.author
                this.dataForm.name = data.researchReport.name
                this.dataForm.url = data.researchReport.url
                this.dataForm.createBy = data.researchReport.createBy
                this.dataForm.createTime = data.researchReport.createTime
                this.dataForm.updateBy = data.researchReport.updateBy
                this.dataForm.updateTime = data.researchReport.updateTime
                this.dataForm.delFlag = data.researchReport.delFlag
                this.dataForm.version = data.researchReport.version
                this.dataForm.corpCode = data.researchReport.corpCode
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/achievements/researchreport/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'author': this.dataForm.author,
                'name': this.dataForm.name,
                'url': this.dataForm.url,
                'createBy': this.dataForm.createBy,
                'createTime': this.dataForm.createTime,
                'updateBy': this.dataForm.updateBy,
                'updateTime': this.dataForm.updateTime,
                'delFlag': this.dataForm.delFlag,
                'version': this.dataForm.version,
                'corpCode': this.dataForm.corpCode
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
